fix: prevent term-select from overriding current term on initial load#198
fix: prevent term-select from overriding current term on initial load#198
Conversation
Greptile SummaryThis PR fixes a race condition on initial page load where
Confidence Score: 4/5Safe to merge; the fix resolves the described race condition with a minor edge-case consideration. Single-line, targeted change with clear rationale backed by the
|
| Filename | Overview |
|---|---|
| src/components/classes/list/components/term-select.tsx | Adds selectedTermId && guard to prevent the fallback effect from overriding initial term selection on load |
Reviews (1): Last reviewed commit: "fix: prevent term-select from overriding..." | Re-trigger Greptile
| useEffect(() => { | ||
| const selectedTerm = terms?.find((t) => t.id === selectedTermId); | ||
| if (!selectedTerm && terms?.[0] && !isRefetching) | ||
| if (selectedTermId && !selectedTerm && terms?.[0] && !isRefetching) | ||
| setSelectedTermId(terms[0].id); | ||
| }, [terms, selectedTermId, setSelectedTermId]); |
There was a problem hiding this comment.
Potential regression when no current term exists
The guard is correct for the described race condition, but it removes a safety net for a related edge case. When queryTerm === "current" but no term is marked as current (i.e., currentTerm is undefined), the effect in class-list-view.tsx (lines 112–116) never sets selectedTermId, leaving it as null indefinitely. Previously this useEffect would have rescued the UI by falling back to terms[0]; with selectedTermId && added, the guard prevents that fallback and the component renders a loading skeleton forever.
If it's always guaranteed that a current term exists when there are any terms, this is fine. Otherwise, consider adding a fallback in class-list-view.tsx for when queryTerm === "current" but currentTerm is absent:
// In class-list-view.tsx, inside the sync effect (around line 112)
if (queryTerm === "current") {
if (!selectedTermId && currentTerm) {
setSelectedTermId(currentTerm.id);
} else if (!selectedTermId && !isLoadingCurrentTerm && terms?.[0]) {
// No current term configured; fall back to the first term
setSelectedTermId(terms[0].id);
}
return;
}
term-select.tsxhad a fallback effect that ran when selectedTermId was null, racing against the current term fetch in class-list-view.tsx and overwriting it with terms[0].